package com.transaction.demo.order.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

import com.transaction.demo.common.enums.ResponseExceptionEnum;
import com.transaction.demo.common.util.JsonUtils;
import com.transaction.demo.common.util.NotifyDestinationNameEnum;
import com.transaction.demo.common.util.Result;
import com.transaction.demo.common.util.StringUtil;
import com.transaction.demo.message.MessageService;
import com.transaction.demo.message.vo.req.TransactionMessageAddReqVo;

/**
 * 描述：支付回调通知
 * 作者：马庆智
 * 日期：2023/5/5 13:47
 */
@Api(tags = "支付回调通知")
@RestController
@RequestMapping("/v1")
@Slf4j
public class PayNotifyController {

	@Autowired
	private MessageService messageService;

	/**
	 * 支付回调通知
	 *
	 * @param request 请求参数
	 * @return String
	 * @author 马庆智
	 * @date: 2023/5/5 13:49
	 */
	@ApiOperation("支付回调通知")
	@PostMapping("/notify")
	public String notify(HttpServletRequest request) {
		//1.获取请求参数
		Map<String, String> params = new HashMap<>();
		Map<String, String[]> requestParams = request.getParameterMap();
		for (String name : requestParams.keySet()) {
			String[] values = requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
			}
			//乱码解决，这段代码在出现乱码时使用。
			//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
			params.put(name, valueStr);
		}

		log.info("接收到支付回调通知的参数:{}", JsonUtils.toJson(params));

		//2.参数签名

		//3.订单处理消息
		String messageId = StringUtil.get32UUID();
		params.put("messageId", messageId);
		TransactionMessageAddReqVo messageAddReqVo = new TransactionMessageAddReqVo();
		messageAddReqVo.setMessageId(messageId);
		messageAddReqVo.setMessageBody(JsonUtils.toJson(params));
		messageAddReqVo.setConsumerQueue(NotifyDestinationNameEnum.BANK_NOTIFY.name());

		Result<Integer> result = messageService.saveAndSendMessage(messageAddReqVo);
		log.info("订单处理响应:{}", JsonUtils.toJson(result));

		if (!result.getCode().equals(ResponseExceptionEnum.SUCCESS.getCode())) {
			log.info("订单处理失败:{}", JsonUtils.toJson(result));
			return "error";
		}

		//4.通知商户
		//if (result.getData() > 0) {
		//	String notifyMessageId = StringUtil.get32UUID();
		//	params.put("messageId", notifyMessageId);
		//	TransactionMessageAddReqVo notifyMessageAddReqVo = new TransactionMessageAddReqVo();
		//	notifyMessageAddReqVo.setMessageId(notifyMessageId);
		//	notifyMessageAddReqVo.setMessageBody(JsonUtils.toJson(params));
		//	notifyMessageAddReqVo.setConsumerQueue(NotifyDestinationNameEnum.MERCHANT_NOTIFY.name());
		//	log.info("通知商户通知参数:{}", JsonUtils.toJson(notifyMessageAddReqVo));
		//	messageService.directSendMessage(notifyMessageAddReqVo);
		//}

		return "success";
	}
}
